package com.shxt.controller;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.shxt.model.Menu;
import com.shxt.model.Role;
import com.shxt.service.MenuService;
import com.shxt.service.RoleService;
import com.shxt.util.PageBean;
@Controller
public class RoleController {
	@Autowired
	private RoleService roleService;
	@Autowired
	private MenuService menuService;
	@RequestMapping("/user/listRole")
	public String roleList(Role role,Map<String,Object> map,@RequestParam(required=false,defaultValue="1")int pages,String message){
		if(pages==0){
			pages=1;
		}
		map = PageBean.serverMap(map , role , pages);
		List<Role> roleList=  this.roleService.listRole(map);
		int count =  this.roleService.count(map);
		//向前台传递分页信息
		map = PageBean.map(map , role.getClass() , roleList , count , pages);
		map.put("url","user/listRole");
		map.put("message", message);
		return "/user/listRole";
	}
	@PostMapping("role/delRole")
	/**
	 * 删除角色
	 * **/
	public String delRoles(Integer role_id,String p1,Map<String , Object> map){
		System.out.println();
		int result = this.roleService.delete(role_id);
		if(result>0){
			map.put("message" , "删除成功") ;
		}else{
			map.put("message" , "删除失败") ;
		}
		map.put("pages", p1);
		return "redirect:/user/listRole";
	}
	/**
	 * 添加角色
	 * **/
	@PostMapping("role/addRole")
	public String addRole(Role role,Map<String , Object> map){

		int result = this.roleService.add(role);
		if(result>0){
			map.put("message" , "添加成功") ;
		}else{
			map.put("message" , "添加失败") ;
		}
		return "redirect:/user/listRole";
	}
	/**
	 * 分配资源
	 * **/
	@GetMapping("role/mdiMenu")
	public String mdiMenu(Integer role_id,Map<String , Object> map){

		map.put("role_id" , role_id);
		List<Menu> menuList = this.menuService.selMenubyRoleId(role_id);
		List<Menu> menuNoList = this.menuService.selMenubyNoRoleId(role_id);
		map.put("menuList" , menuList);
		map.put("menuNoList" , menuNoList);
		return "user/mdiMenu";
	}
	@PostMapping("role/mdiMenu")
	public String mdiMenus(Integer role_id,Integer[] selectR,Map<String , Object> map){
		this.menuService.deleteMenu(role_id);
		if(selectR!=null&&selectR.length>0){
			map.put("role_id" , role_id);
			map.put("menu_ids" , selectR);
			this.menuService.addMenus(map);
		}
		map.put("message" , "分配资源成功");
		return "jsp/message";
	}
}
